<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use app\models\GameUser;
use app\models\MorningRecord;
/**
 * This is the model class for table "blxbgame_morning".
 *
 * @property integer $id
 * @property integer $userid
 * @property integer $signdays
 * @property integer $totalscore
 * @property string  $updatetime
 */
class Morning extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName(){
        return "{{%morning}}";
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid'], 'required'],
            [['signdays', 'totalscore'], 'integer'],
            [['time'], 'safe'],
            [['userid'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '起床表序号',
            'userid' => '用户序号',
            'signdays' => '签到天数',
            'onsign' => '连续签到天数',
            'totalscore' => '总分',
            'time' => '签到时间',
            'msectime' => '精确时间',
        ];
    }

    public function data_by_id($userid){
        $data = Morning::find()->where('userid = :userid',[':userid'=>$userid] )->asArray()->one();       
        return $data;
    }

    public function firstsign($userid){
        $data = $data = Morning::find()->where('userid = :userid',[':userid'=>$userid] )->asArray()->one();
        if( empty($data) ){
            $sign = 'N';
        }else{
            $sign = 'Y';     
        }
        return $sign;
    }

    public function add($data){
        $morning = new Morning();
        if($morning->load($data,'') && $morning->save()){
            return true;
        }
        return false;

    }

    public function signdaysUpdate($data){
        $key = [
            'userid' => $data['userid'],
        ];
        $changedata = [
            'onsign' => $data['onsign'],
        ];
        $result = Morning::updateAll($changedata,$key);
        if( $result != 1 ){
            $result = -1;
        }else{
            $result = 1;
        }
        return $result;
    }

    public function signupUpdate($data){
        $key = [
            'userid' => $data['userid'],
        ];
        $changedata = [
            'signdays' => $data['signdays'],
            'onsign' => $data['onsign'],
            'totalscore' => $data['totalscore'],
            'shopscore' => $data['shopscore'],
            'time' => $data['datetime'],
            'msectime' => $data['msectime'],
        ];
        $result = Morning::updateAll($changedata,$key);
        if( $result != 1 ){
            $result = -1;
        }else{
            $result = 1;
        }
        return $result;
    }

    public function scoreUpdate($data){
        $key = [
            'userid' => $data['userid'],
        ];
        $changedata = [
            'shopscore' => $data['newscore'],
        ];
        $result = Morning::updateAll($changedata,$key);
        if( $result != 1 ){
            $result = -1;
        }else{
            $result = 1;
        }
        return $result;
    }
    
    //起床时间
    //签到排名
    public static function getMorningRank($time,$msectime){
        date_default_timezone_set('Asia/Shanghai');
        $today = date ( 'Y-m-d' );
        $count = Morning::find()->where('(time >= :today and time < :time) OR (time = :time  AND msectime < :msectime) ',[':today'=>$today,':time'=>$time,':msectime'=>$msectime])->count();
       return $count+1;
    }
    //签到时间
    public function getMorningTime($userid){
        date_default_timezone_set('Asia/Shanghai');
        $today = date ( 'Y-m-d' );
        $morningTime = Morning::find()->where('(time >= :today AND userid = :userid) ',[':today'=>$today,':userid'=>$userid])->asArray()->one();
        if( empty($morningTime) ){
            $morningTime = -1;
        }
        return $morningTime;
    }
    //前10起床排名
    public function TopTenbydatatime(){
        date_default_timezone_set('Asia/Shanghai');
        $today = date ( 'Y-m-d' );
        $nextday = date( "Y-m-d",time()+24*60*60);
        $data  = Morning::find()->where('time >= :time1 and time < :time2',[':time1'=>$today,':time2'=>$nextday])->orderBy('time asc , msectime asc')->limit(30)->asArray()->all();
        $i=0;
        $arr = Null;
        foreach ($data  as $value){
            $res = GameUser::data_by_id($value['userid']);
            //毫秒处理
            $msectime = $value['msectime']/1000000;
            $msectime = round($msectime,3);
            $msectime = sprintf("%01.3f",$msectime);
            list($usec, $msectime) = explode(".",$msectime);
            $msectime = str_pad($msectime,3,"0",STR_PAD_RIGHT);
            //时间处理
            $time = explode(' ', $value['time']);
            $time = $time[1]. '.' . $msectime;;
            $arr[$i]=array(
                'rank' => $i+1,
                'nickname'=> $res['nickname'],
                'avatarurl' => $res['avatarUrl'],
                'time'=> $time,
            );
            $i++;
        }
        return $arr;
    }
    //后8起床排名
    public function LastTenbydatatime(){
        date_default_timezone_set('Asia/Shanghai');
        $today = date ( 'Y-m-d' );
        $nextday = date( "Y-m-d",time()+24*60*60);
        $data  = Morning::find()->where('time >= :time1 and time < :time2',[':time1'=>$today,':time2'=>$nextday])->orderBy('time desc , msectime desc')->limit(8)->asArray()->all();
        $i = count($data)-1;
        $arr = Null;
        foreach ($data  as $value){
            $res = GameUser::data_by_id($value['userid']);
            $time = explode(' ', $value['time']);
            $time = $time[1];
            $arr[$i]=array(
                'avatarurl' => $res['avatarUrl'],
            );
            $i--;
        }
        if($arr == Null){
            $arr = "";
        }
        return $arr;
    }
    
    //积分
    public function getTotalScoreRank($totalscore ,$time,$msectime){
        $count = Morning::find()->where('totalscore > :totalscore OR (totalscore = :totalscore AND time < :time) OR (totalscore = :totalscore AND time =:time AND msectime < :msectime)' ,[':totalscore'=>$totalscore,':time'=>$time,':msectime'=>$msectime])->count();
        $count = $count+1;
        return $count;
    }
    
    //积分前30排行榜
    public static function ToptenbyScore(){
        $data  = Morning::find()->where('totalscore > 0')->orderBy('totalscore desc')->asArray()->all();
        $count = count($data);
        for ($i=0;$i<$count;$i++){
            for($j=$i+1;$j<$count;$j++){
                if($data[$i]['totalscore'] == $data[$j]['totalscore'] && $data[$i]['time'] > $data[$j]['time'] ){
                    $change = Null;
                    $change = $data[$i];
                    $data[$i] = $data[$j];
                    $data[$j] = $change;
                    $change = Null;
                }else if($data[$i]['totalscore'] == $data[$j]['totalscore'] && $data[$i]['time'] == $data[$j]['time']){
                    if($data[$i]['msectime'] > $data[$j]['msectime']){
                        $change = Null;
                        $change = $data[$i];
                        $data[$i] = $data[$j];
                        $data[$j] = $change;
                        $change = Null;
                    }
                }
            }
        }
        $i = 0;
        $arr = Null;
        foreach ($data  as $value){
            if($value['totalscore'] > 0){
                $res = GameUser::data_by_id($value['userid']);
                $arr[$i]=array(
                    'rank' => $i+1,
                    'nickname'=> $res['nickname'],
                    'avatarurl' => $res['avatarUrl'],
                    'totalscore'=> $value['totalscore'],
                );
            $i++;
            }
        }
       if(!empty($arr)){
          $arr = array_slice($arr, 0, 30);
       } 
       return $arr;
    }



}
